/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.battletank.game;

import com.nokia.example.battletank.Main;
import com.nokia.example.battletank.game.entities.Enemy;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Image;

public class Levels {
    private static final String PATH = "/levels/";
    private static final String DRM_PATH = "/drm/data/ResourceId_";
    private static final String[] fileNames = {
        "first.png",
        "classic.png",
        "tight.png",
        "easy.png",
        "mario.png",
        "mega.png",
        "bob.png"
    };

    public static Image getImage(int level) throws ProtectedContentException, IOException {
        System.out.println(Main.isTrial());
        String fileName = fileNames[(level-1)%fileNames.length];
        if(level <= 2) {
            return Image.createImage(PATH + fileName);
        } else if (Main.isTrial()) {
            throw new ProtectedContentException();
        } else {
            try {
                InputStream input = Main.getIAPManager().getDRMResourceAsStream(DRM_PATH+Main.PURCHASE_ID+"/"+fileName);                
                return Image.createImage(input);
            } catch(Exception e) {                
                throw new IOException();
            }
        }
    }

    public static int getTotalEnemies(int level) {
        return Math.min(2 + level/2 * 2, 20);
    }

    public static int getConcurrentEnemies(int level) {
        return Math.min(2 + level/4, 6);
    }

    public static int getEnemyType(int level, int remainingEnemies) {
        int enemy = getTotalEnemies(level) - remainingEnemies;
        if(enemy < 3) return Enemy.BASIC;
        else if(enemy%3 == 0) return Enemy.FAST;
        else if(enemy%3 == 1) return Enemy.BASIC;
        else return Enemy.HEAVY;
    }
}
